package mf.com.controller.sys;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import mf.com.Service.sys.SysUserService;
import mf.com.dto.system.AssginRoleDto;
import mf.com.dto.system.SysUserDto;
import mf.com.entity.system.SysUser;
import mf.com.vo.common.Result;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: lmf
 * @Create: 2024/3/28 21:51
 * Description:  用户相关控制层
 */
@RestController
@RequestMapping("/admin/system/sysUser")
@Tag(name = "用户相关")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    @Operation(summary = "用户分页查询")
    public Result<PageInfo<SysUser>> findByPage(@PathVariable("pageNum") Integer current,
                                                @PathVariable("pageSize") Integer limit, SysUserDto sysUserDto) {
        System.out.println(1);
        return Result.success(sysUserService.findByPage(sysUserDto, current, limit));
    }

    @PostMapping("/saveSysUser")
    @Operation(summary = "新增用户")
    public Result<Integer> saveSysUser(@RequestBody SysUser sysUser) {
        System.out.println(sysUser);
        return Result.success(sysUserService.saveUser(sysUser));
    }

    @PutMapping("/updateSysUser")
    @Operation(summary = "修改用户")
    public Result<Integer> updateSysUser(@RequestBody SysUser sysUser) {
        return Result.success(sysUserService.updateUser(sysUser));
    }

    @DeleteMapping("/deleteById/{userId}")
    @Operation(summary = "删除用户")
    public Result<Integer> deleteById(@PathVariable("userId") Integer userId) {
        return Result.success(sysUserService.deleteUser(userId));
    }

    @GetMapping("/getByName/{userName}")
    @Operation(summary = "根据名称查询用户")
    public Result<SysUser> getById(@PathVariable("userName") String userName) {
        return Result.success(sysUserService.findByName(userName));
    }

    @PostMapping("/doAssign")
    @Operation(summary = "为用户分配角色")
    public Result<Void> doAssign(@RequestBody AssginRoleDto assginRoleDto){
        sysUserService.doAssign(assginRoleDto);
        return Result.success(null);
    }
}
